/*
 * GPDMA.c
 *
 *  Created on: 09-02-2012
 *      Author: Mati
 */
#include "GPDMA.h"
#define TRANSFER_SIZE (NUM_OF_ADC_SAMPLES)
extern uint32_t ADC_Meas[NUM_OF_ADC_SAMPLES];
/*------------------------------------------------------------------------------
  configer GPDMA
 *------------------------------------------------------------------------------*/

void GPDMA_Config(void) {
	LPC_SC->PCONP |= (1U<<29);						//power up DMA
	LPC_GPDMA->DMACConfig |= (1U<<0);				//enable the DMA, 1<<1 for big endian, default: little endian
	LPC_GPDMA->DMACSync   |= (1U<<10); 				//use MAT1.0 for Sync
	LPC_SC->DMAREQSEL     |= (1U<<2); 				//Timer1 Match Compare 0 as DMA request

	LPC_GPDMA->DMACIntErrClr  |= 0xff;				//Clear the Interrupt Error Status register, 1 - clear
	LPC_GPDMA->DMACIntTCClear |= 0xff; 				//Clear the Interrupt Terminal Count Request
}

void SetUpNewDMATransferFromADC(uint16_t addr)
{
	//Set the source and destination addresses
		LPC_GPDMACH0->DMACCSrcAddr  = (uint32_t) &(LPC_ADC->ADDR5); //source of data - ADC value
		LPC_GPDMACH0->DMACCDestAddr = (uint32_t) & ADC_Meas[addr]; 			//save ADC samples to array

		LPC_GPDMACH0->DMACCLLI = 0;						//Clear the Linked List as we are not using it

		//Set the burst transfer size, source burst and destination burst size, source and destination width,
		//source increment, destination increment
		/*In our case, let the transfer size be 200 bytes, source burst and destination burst sizes are 1,
		 source and destination width are 8-bits and we need to do source increment and there is no need
		 for destination increment */
		LPC_GPDMACH0->DMACCControl = ((TRANSFER_SIZE)<<0)| 	//Transfer Size: 2xNUM_OF_SAMPLES B (bo uint16_t)
									 (0U<<12) |				//Source Burst Size: 000->1
									 (0U<<15) |				//Destination Burst Size: 000->1
									 (0b010U<<18) |			//Source Transfer Width: 010->4B
									 (0b010U<<21) |			//Destination Transfer Width: 010->4B
									 (0U<<26) |				//Source Increment
									 (1U<<27);				//Destination Increment

		//Set the type of transfer as Memory to Peripheral and the destination request line as MAT1.0
		LPC_GPDMACH0->DMACCConfig = (0x1U)<<0 |				//enable channel
									(0x04U<<1)|				//Source Perpherial ADC
									(0x02U<<11); 			//Transfer Type: 010 -> PER2MEM
}

// działa cos
/*void SetUpNewDMATransferFromADC(uint16_t addr)
{
	//Set the source and destination addresses
		LPC_GPDMACH0->DMACCSrcAddr  = (uint32_t) &(LPC_ADC->ADDR5); //source of data - ADC value
		LPC_GPDMACH0->DMACCDestAddr = (uint32_t) & ADC_Meas[addr]; 			//save ADC samples to array

		LPC_GPDMACH0->DMACCLLI = 0;						//Clear the Linked List as we are not using it

		//Set the burst transfer size, source burst and destination burst size, source and destination width,
		//source increment, destination increment
		In our case, let the transfer size be 200 bytes, source burst and destination burst sizes are 1,
		 source and destination width are 8-bits and we need to do source increment and there is no need
		 for destination increment
		LPC_GPDMACH0->DMACCControl = ((TRANSFER_SIZE)<<0)| 	//Transfer Size: 2xNUM_OF_SAMPLES B (bo uint16_t)
									 (0U<<12) |				//Source Burst Size: 000->1
									 (0U<<15) |				//Destination Burst Size: 000->1
									 (0x01U<<18) |			//Source Transfer Width: 001->2B
									 (0x01U<<21) |			//Destination Transfer Width: 001->2B
									 (0U<<26) |				//Source Increment
									 (1U<<27);				//Destination Increment

		//Set the type of transfer as Memory to Peripheral and the destination request line as MAT1.0
		LPC_GPDMACH0->DMACCConfig = (0x1U)<<0 |				//enable channel
									(0x04U<<1)|				//Source Perpherial ADC
									(0x02U<<11); 			//Transfer Type: 010 -> PER2MEM
}*/
